#ifndef CLIENTINSTANCE_HH
#define CLIENTINSTANCE_HH

#include "../library/thread.hh"
#include "../library/mutex.hh"
#include "../library/protocol.hh"
#include "../library/adistributedmodule.hh"

#include "clientcounter.hh"
#include <dlfcn.h>
#include <sstream>

using namespace std;
using namespace NM;
using namespace DLIB_PROTO;

class ClientInstance : Thread, Mutex
{
private: // variables
    bool            _run;                       // Mutex protected
    Socket          *_network;
    Protocol        _client;
    clientCounter   *_counter;
    string          _moduleName;
    void            *_moduleHandle;

public: // interface
    ClientInstance();
    ClientInstance(Socket *network, clientCounter *counter);
    ~ClientInstance();

    void Start();
    void Stop();

    void execute();                             // DO NOT CALL (thread reserved)
    bool IsRunning();

private: // internals
    bool init();
    void setRunning(bool run);

    bool saveModule();
    ADistributedModule *getModuleInstance();
    bool executeModule(ADistributedModule *moduleInstance);
};

void *client_instance_starter(void *data);

#endif // CLIENTINSTANCE_HH
